#include "GetRequest.h"
			//John Sabo's test comment
GetRequest::GetRequest() {
    method = "GET";
    uri = "";
    version = "HTTP/1.1";
}

GetRequest::GetRequest(stringstream& s) : Request(s) { //runs Request(s) logic, then GetRequest(s) logic
    method = "GET";
}

GetRequest GetRequest::operator =(GetRequest& rhs) {
    method = rhs.method;
    uri = rhs.uri;
    version = rhs.version;
    headers = rhs.headers; // May need to iterate through headers and add in items.
    return *this;
}
Response GetRequest::processRequest()
{
    Response resp;
    int fileNameStartPosition = uri.find_first_of('/');
    resp.fileName = uri.substr(fileNameStartPosition);
    char buf[1024];
    getcwd(buf,1024);
    string cwd = string(buf);
    resp.method = "GET";

    // Determine if file exists.
    resp.responseFile.open((cwd + basedir + resp.fileName).c_str(), ios::in|ios::binary|ios::ate );

    string fileExtension, contentType;
    if (resp.responseFile != NULL)
    {
        // File found.
        // Determine file type for header.
        int fileExtensionStartPosition = resp.fileName.find_last_of('.');
        fileExtension = resp.fileName.substr(fileExtensionStartPosition);
        resp.statusCode = "200";
        resp.reasonPhrase = "OK";

        
        
        // Build response header.
    // Status line.
    //this->responseHeader.seekg(0, ios::beg);
    }
    else
    {        
        // Requested file was not found.
        resp.fileName = "/404.html";
        resp.responseFile.open((cwd + basedir + resp.fileName).c_str(), ios::in|ios::binary|ios::ate );
        resp.statusCode = "404";
        resp.reasonPhrase = "Not Found";
        resp.responseLength = 0;
    }
    if (fileExtension == ".html" || fileExtension == ".htm")
    {
        contentType = "text/html";
    }
    else if (fileExtension == ".txt")
    {
        contentType = "text/plain";
    }
    else if (fileExtension == ".xml")
    {
        contentType = "text/xml";
    }
    else if (fileExtension == ".jpeg" || fileExtension == ".jpg")
    {
        contentType = "image/jpeg";
    }
    else if (fileExtension == ".png")
    {
        contentType = "image/png";
    }
    else if (fileExtension == ".gif")
    {
        contentType = "image/gif";
    }

    resp.headers << "Content-Type: " << contentType << "\r\n";

        // Get the length of the file.*/
        // size_t responseFileSize = 0;
    resp.responseFile.seekg(0, ios::end);
    resp.responseLength = resp.responseFile.tellg();
    resp.headers << "Content-Length: " << resp.responseLength << "\r\n";
    resp.responseFile.seekg(0, ios::beg);
    resp.headers << "Content-Encoding: " << "identity\r\n";
    resp.MakeHeader();
    resp.responseFile.close();
    return resp;
}
void GetRequest::PrintInfo() {
    Request::PrintInfo();
    cout << "This is a GetRequest Object." << endl;
}
